package com.zszedu.web.controller;

import com.zszedu.constants.RedisKeyConstants;
import com.zszedu.pojo.Page;
import com.zszedu.pojo.Product;
import com.zszedu.service.ProductService;
import com.zszedu.web.config.RedisConfig;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("product")
public class ProductController {
    @DubboReference
    private ProductService productService;

    @Autowired
    private RedisConfig redisConfig;

    //根据id查询数据
    @GetMapping({"{id}"})
    public Product queryById(@PathVariable Integer id){
        String key = "myRecords:personal:" + id;
        ValueOperations redis = redisConfig.getRedis();
        Product product = (Product) redis.get(key);

        if (product == null){
            product = productService.queryById(id);
            redis.set(key,product);
        }

        return product;
    }

    @GetMapping("rate")
    public Double AvgRate() {
        ValueOperations redis = redisConfig.getRedis();
        //从Redis中获取 name 为 rate 的数据
        Double rate = (Double) redis.get(RedisKeyConstants.REDIS_RATE);
        //该数据为空，则创建数据存储进入Redis中，并且设置保存时间
        if (rate == null) {
            rate = productService.getAvgRate();
            // 存储时间为 1 小数
            redis.set(RedisKeyConstants.REDIS_RATE, rate, 1, TimeUnit.HOURS);
        }

        return rate;
    }

    //根据产品类型type以及产品期限cycle查询信息，只返回一个对象
    @GetMapping("/{type}/{cycle}")
    public Product queryByTypeAndCycle(@PathVariable Integer type, @PathVariable Integer cycle) {
        ValueOperations redis = redisConfig.getRedis();

        String key = "Product:" + type + "/" + cycle;
        Product product = (Product) redis.get(key);

        //从Redis中获取的数据为空，则查询数据库并存储进Redis中
        if (product == null) {
            product = productService.queryByTypeAndCycle(type, cycle);
            redis.set(key, product, 1, TimeUnit.HOURS);
        }

        return product;
    }

    //根据产品类型(productType)/产品状态(productStatus)并且按照金额排序(从小到大)，返回固定的长度 limit
    @GetMapping("/top{type}/{limit}")
    public List queryByTypeAndStatus(@PathVariable Integer type, @PathVariable Integer limit) {
        ValueOperations redis = redisConfig.getRedis();

        String key = "Product:top:" + type + "/" + limit;
        List list = (ArrayList) redis.get(key);

        //从Redis中获取的数据为空，则查询数据库并存储进Redis中
        if (list == null) {
            list = productService.queryByTypeAndStatus(type, limit);
            redis.set(key, list, 1, TimeUnit.HOURS);
        }

        return list;
    }

    //分页器，根据传入的参数
    @GetMapping("{type}/page/{page}")
    public Page queryPage(@PathVariable Integer type, @PathVariable Integer page) {
        ValueOperations redis = redisConfig.getRedis();
        String key = "Page:" + type + page;
        Page queryPage = (Page) redis.get(key);

        //从Redis中获取的数据为空，则查询数据库并存储进Redis中
        if (queryPage == null) {
            queryPage = productService.queryPage(type, page);
            redis.set(key, queryPage, 1, TimeUnit.HOURS);
        }

        return queryPage;
    }
}
